package com.huge.simple.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.*;

/**
 * @author jihu
 * @date 2020/9/2
 */
@Configuration
public class ThreadPoolConfig {

    //核心线程数
    private final static Integer corePoolSize = 50;
    //最大线程数
    private final static Integer maximumPoolSize = 200;
    //非核心线程数核心时间
    private final static Long keepAliveTime = 10L;
    //非核心线程数核心时间单位
    private final static TimeUnit unit = TimeUnit.SECONDS;
    //阻塞队列
    private final static BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>(100000);
    //线程创建工厂
    private final static ThreadFactory threadFactory = Executors.defaultThreadFactory();
    //拒绝策略 抛掉最先执行的任务
    private final static RejectedExecutionHandler handler = new ThreadPoolExecutor.DiscardOldestPolicy();


    @Bean("executorService")
    public ExecutorService executor(){
        ThreadPoolExecutor service =
                new ThreadPoolExecutor(
                        corePoolSize,
                        maximumPoolSize,
                        keepAliveTime,
                        unit,
                        workQueue,
                        threadFactory,
                        handler);
        return service;
    }
}
